<?php

/**
 * redis连接池
 * Class RedisPool
 */

class RedisPool {

	private static $connection = array();

	private static $servers = array();

	/**
	 * 添加redis连接
	 * @example $redisPool->addServer('my_redis',['host'=>'127.0.0.1','port'=>6379]);
	 * @param $key
	 * @param $option
	 */
	public function addServer($key,$option){
		self::$servers[$key]['host'] = $option['host'];
		self::$servers[$key]['port'] = $option['port'];
	}

	/**
	 * 获取redis连接
	 * @example $redis = $redisPool::instance('my_redis');
	 * @param $key
	 * @return Redis
	 */
	public static function  instance($key) {

		if(!array_key_exists($key,self::$servers)){
			die('redis连接: "'.$key.'" 不存在!'.PHP_EOL);
		}
		if(!array_key_exists($key,self::$connection)){
			self::$connection[$key] = new Redis();
			self::$connection[$key]->connect(self::$servers[$key]['host'],self::$servers[$key]['port']);
		}
		return self::$connection[$key];
	}

}